<!-- 这里是用来从别的地方进入小程序，重定向地址的页面 -->
<template>
  <view class="container">
    <view class="loading-content">
      <u-loading mode="circle"></u-loading>
      <view class="loading-tect-content">
        <text class="loading-text">加载中，请稍后</text>
      </view>
    </view>
  </view>
</template>
<script lang="ts" setup>
import { onLoad } from '@dcloudio/uni-app';

import { useUserStore } from '@/stores/user';
import { getAppId } from '@/utils/env';

const appid = getAppId();

const userStore = useUserStore();
console.log('--');
onLoad((query: any) => {
  const q = decodeURIComponent(query.q); // 获取到二维码原始链接内容
  console.log(q, '----别的地方跳转');
  // TODO：后期可以根据不同地方的跳转进行不同逻辑操作。
  wx.login({
    success(code: any) {
      enterLogin(code);
    },
  });
});
const enterLogin = async (code: any) => {
  try {
    // throw new Error('---');
    await userStore.login({
      ...toRaw({
        grant_type: 'wechat',
        appid,
        code: code.code,
      }),
    });
    (uni as any).$Router.switchTab({
      name: 'home',
    });
  } catch (err: any) {
    console.log(err, 'errr');
    (uni as any).$Router.push({
      name: 'login',
    });
  }
};
</script>
<style lang="scss">
.container {
  width: 100%;
  height: 100%;
  padding-top: 50rpx;
}
.loading-content {
  text-align: center;
  width: 100%;
}
</style>
